<?php

function smarty_function_yuihmenu($params, &$smarty)
{

	$content = $params['Content'];
	$user_name = $params['User'];
        
	$basefile = $params['BaseFile'];

//print_r($content);

	$out = '';

        $out .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"../../build/menu/assets/menu.css\">";

        $out .="        <!-- Container source file -->
        <script type=\"text/javascript\" src=\"../../build/container/container_core.js\"></script>

        <!-- Menu source file -->
        <script type=\"text/javascript\" src=\"../../build/menu/menu.js\"></script>";

	
/*	$out = "<script type=\"text/javascript\">

            YAHOO.example.onMenuBarReady = function() {

                // Instantiate and render the menu bar

                var oMenuBar = new YAHOO.widget.MenuBar(\"topmenu\", { autosubmenudisplay:true, hidedelay:750, lazyload:true });

                oMenuBar.render();

            };


            // Initialize and render the menu bar when it is available in the DOM

            YAHOO.util.Event.onContentReady(\"topmenu\", YAHOO.example.onMenuBarReady);

        </script>";*/

        $out .="<script type=\"text/javascript\">

            YAHOO.example.onMenuBarReady = function() {
                
                // \"beforerender\" event handler for the menu bar

                function onMenuBarBeforeRender(p_sType, p_sArgs, p_oMenu) {";
				
				$submenu = '';				

                        $count = 0;
                        foreach($content as $item) {
	                        if($item->parent ==0) {
//	                                $out .= "\"$item->value\": "; //"[\n";
	                                $c = 0;
	                                
	                                $subs = '';
	                                
	                                foreach($content as $child) {
	                                        if($child->parent == $item->id) {
	                                                $subs .= "{ text: \"$child->value\", url: \"$basefile?go=$child->id\" },\n";
	                                                $c++;
	                                        }
	                                }
	                                	                                
//	                                $out .= "\t\t\t]," .
									if($subs) {
										$subs = "\"$item->value\": [$subs]";
									}
	                                $submenu .= "$subs\n";
	                                $childs [$count] = array($c,$item->value);
	                                $count++;
	                        }
                        }                        
    
//                $out .="};";
//                $out .="\n";
                $c = 0;
                foreach($childs as $i) {
                        if($i[0]) {
                                $submenu .= "this.getItem($c).cfg.setProperty(\"submenu\", { id:\"$i[1]\", itemdata: oSubmenuData[\"$i[1]\"] });";                      
                        }
                        $c++;
                }

				if($submenu) {
					$out .= "var oSubmenuData = {\n$submenu\n};\n}";
				}
				$out .= "
                // Instantiate and render the menu bar

                var oMenuBar = new YAHOO.widget.MenuBar(\"topmenu\", { autosubmenudisplay:true, showdelay:250, hidedelay:750, lazyload:true });


                // Subscribe to the \"beforerender\" event

                oMenuBar.beforeRenderEvent.subscribe(onMenuBarBeforeRender, oMenuBar, true);


                // Render the menu bar

                oMenuBar.render();
                
            };


            // Initialize and render the menu bar when it is available in the DOM

            YAHOO.util.Event.onContentReady(\"topmenu\", YAHOO.example.onMenuBarReady);

        </script>";


        $out .= "<div id=\"topmenu\" class=\"yuimenubar\">
                            <div class=\"bd\">
                                <ul class=\"first-of-type\">";

        $f = 0;

        foreach ($content as $item) {
                if($item->parent == 0 ) {
//                print_r($item)."<br>";
                        $class = ($f)?('yuimenubaritem'):('yuimenubaritem first-of-type');
                        $out .= "<li class=\"$class\"><a href=\"admin.php5?go=$item->id\">$item->value</a></li>";
                        $f++;  
                }
        }
//                                        <li class=\"yuimenubaritem first-of-type\"><a href=\"http://communication.yahoo.com\">Communication</a></li>
                            
        $out .= "               </ul>
                            </div><div style=\"position:absolute;top: 1px;right: 1px;  font-size: 90%;\">Вы вошли как: <a href=\"/cubes/admin/admin.php5?go=17\">$user_name</a></div>
                        </div>";
       
	return $out;
} 

/* vim: set expandtab: */

 
?>
